import java.util.Scanner;
public class Admin extends User{
    public static final Admin DEFAULT_ADMIN=new Admin("AD001","admin","ynuinfo#777","2000-7-10","13800138000","管理员");

    public Admin(String userID,String userName,String password,String registerTime,String phone,String userType){
        super(userID,userName,password,registerTime,phone,userType);
    }

    @Override
    public void login() {
        Scanner reader = new Scanner(System.in);
        int chance = 3;
        int attempt = 0;
        while (attempt < chance) {
            System.out.println("请输入用户名：");
            String name = reader.next();
            System.out.println("请输入用户密码：");
            String password = reader.next();

            User user = null;
            for (User u : UserList.getAllUsers()) {
                if (u.getUserName().equals(name)) {
                    user = u;
                    break;
                }
            }

            if (user != null && user.getUserPassword().equals(password) && user.getUserType().equals("管理员")) {
                System.out.println("登录成功！欢迎您，" + user.getUserName() + "管理员！");
                Log.info(name, "登录系统", "管理员登录成功");
                show();
                return;
            } else {
                attempt++;
                int remaining = chance - attempt;
                Log.warning(name, "登录系统", "用户名/密码错误或非管理员账户，剩余" + remaining + "次");
                if (remaining > 0) {
                    System.out.println("用户名/密码错误或非管理员账户！剩余" + remaining + "次机会。");
                } else {
                    System.out.println("登录失败次数过多，退出登录。");
                }
            }
        }
    }
    public void modifyOwnPassword() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入原密码：");
        String oldPwd = scanner.nextLine();
        if (!oldPwd.equals(this.getUserPassword())) {
            System.out.println("原密码错误");
            Log.warning(this.getUserName(), "修改自身密码", "原密码错误");
            return;
        }
        System.out.print("请输入新密码：");
        String newPwd = scanner.nextLine();
        this.setUserPassword(oldPwd, newPwd);
        Log.info(this.getUserName(), "修改自身密码", "管理员密码修改成功");
        System.out.println("密码修改成功");
    }

    @Override
    public void exit() {
        Log.info(getUserName(), "退出系统", "管理员返回主菜单");
        System.out.println("已退出管理员系统，返回主菜单");
    }

    @Override
    public void show() {
        while(true){
            System.out.println("=======管理员操作菜单=========");
            System.out.println("1------->密码管理");
            System.out.println("2------->查询用户");
            System.out.println("3------->删除用户");
            System.out.println("4------->增加用户");
            System.out.println("5------->修改用户类型");
            System.out.println("6------->退出");
            System.out.print("请输入1-6的数字选择功能：");
            Scanner reader=new Scanner(System.in);
            int choice;
            try {
                choice = reader.nextInt();
            } catch (Exception e) {
                System.out.println("输入错误，请输入数字！");
                reader.next();
                continue;
            }
            switch (choice) {
                case 1:
                    resetUserPassword();
                    break;
                case 2:
                    listAllUsers();
                    break;
                case 3:
                    deleteUser();
                    break;
                case 4:
                    addUser();
                    break;
                case 5:
                    modifyUserType();
                    break;
                case 6:
                    exit();
                    return;
                default:
                    System.out.println("输入无效，请重新选择！");
            }
        }
    }

    public boolean addUser() {
        Scanner reader=new Scanner(System.in);
        System.out.println("==========添加用户==========");
        System.out.print("请输入用户id");
        String id=reader.next();
        System.out.print("请输入用户名");
        String name=reader.next();
        System.out.print("请输入用户类型（管理员/经理/前台）");
        String userType=reader.next();
        System.out.print("请输入手机号");
        String phone=reader.next();
        for (User user : UserList.getAllUsers()) {
            if (user.getUserId().equals(id)) {
                System.out.println("错误：用户ID已存在！");
                return false;
            }
            if (user.getUserName().equals(name)) {
                System.out.println("错误：用户名已存在！");
                return false;
            }
            if (user.getPhone().equals(phone)) {
                System.out.println("错误：手机号已被使用！");
                return false;
            }
        }
        String registerTime = java.time.LocalDate.now().toString();
        User newUser;
        switch (userType) {
            case "管理员":
                newUser = new Admin(id, name, "123456", registerTime, phone, userType);
                break;
            case "经理":
                newUser = new Manager(id, name, "123456", registerTime, phone, userType);
                break;
            case "前台":
                newUser = new FrontDesk(id, name, "123456", registerTime, phone, userType);
                break;
            default:
                System.out.println("错误：用户类型不存在！");
                return false;
        }
        UserList.addUser(newUser);
        System.out.println("用户【"+name+"】添加成功！默认密码：123456");
        return true;
    }

    public boolean deleteUser() {
        Scanner reader = new Scanner(System.in);
        System.out.print("请输入要删除的用户名：");
        String userName = reader.next();
        User target = null;
        for (User u : UserList.getAllUsers()) {
            if (u.getUserName().equals(userName)) {
                target=u;
                break;
            }
        }
        if (target == null) {
            System.out.println("错误：用户不存在！");
            return false;
        }
        if (target.getUserName().equals("admin")) {
            System.out.println("错误：不能删除默认管理员！");
            return false;
        }
        System.out.print("确定删除用户【" + userName + "】吗？(y/n)：");
        String confirm = reader.next();
        if (confirm.equalsIgnoreCase("y")) {
            UserList.removeUser(target);
            System.out.println("用户删除成功！");
            return true;
        }else{
            System.out.println("已取消删除。");
            return false;
        }
    }

    public boolean resetUserPassword() {
        Scanner reader = new Scanner(System.in);
        System.out.print("请输入要重置密码的用户名：");
        String userName = reader.next();
        User target = null;
        for (User u : UserList.getAllUsers()) {
            if (u.getUserName().equals(userName) && !u.getUserType().equals("管理员")) {
                target = u;
                break;
            }
        }
        if (target == null) {
            System.out.println("错误：用户不存在或为管理员（禁止重置）！");
            return false;
        }
        target.setUserPassword("123456") ;
        UserList.saveToFile();
        System.out.println("用户【" + userName + "】密码已重置为：123456");
        return true;
    }

    public void listAllUsers() {
        System.out.println("\n===== 所有用户列表 =====");
        System.out.println("ID\t用户名\t类型\t注册时间\t手机号");
        System.out.println("----------------------------------------");
        for (User u : UserList.getAllUsers()) {
            System.out.printf("%s\t%s\t%s\t%s\t%s\n",u.getUserId(),u.getUserName(),u.getUserType(),u.getRegisterTime(),u.getPhone());
        }
    }

    public void modifyUserType(){
        Scanner reader = new Scanner(System.in);
        System.out.print("请输入要修改类型的用户名：");
        String userName = reader.next();
        User target=null;
        for (User u:UserList.getAllUsers()) {
            if (u.getUserName().equals(userName) && (u.getUserType().equals("经理") || u.getUserType().equals("前台"))) {
                target=u;
                break;
            }
        }
        if (target==null) {
            System.out.println("错误：用户不存在或类型不是经理/前台！");
            return;
        }
        String newType=target.getUserType().equals("经理") ? "前台" : "经理";
        target.setUserType(newType) ;
        UserList.saveToFile();
        System.out.println("用户【" + userName + "】类型已修改为：" + newType);
    }
}